package code.example.base.lang.number;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(JUnit4.class)
public class Bytes {
    public int byteToInt(byte [] datas){
        if(datas == null ){
            throw new IllegalArgumentException("parameter is null ");
        }else if( datas.length>4){
            throw new IllegalArgumentException("parameter length than 4 ");
        }
        int value = 0;
        int length = datas.length;
        for(int i=0;i<length;i++){
            int v = datas[i] &0xff;
            value |= (v<<((length-i-1)*8));
        }
        return value;
    }

    public byte[] intToBytes(int value){
        byte data [] = new byte [4];
        for(int i=0;i<4;i++){
           byte  x = (byte)((value>>(i*8)) &0xff);
           data[3-i]=x;
        }
        return data;
    }
    @Test
    public void test(){
        int value = 256;
        byte datas [] =  intToBytes(256);
        int valuestr = byteToInt(datas);
         assertThat(valuestr,equalTo(value));
    }
    public static void main(String args []) {
       byte m = (byte)0x80;
       byte k = -5;
        System.out.println(k&0xff);
       System.out.println(m);
        System.out.println(256+k);
    }
}
